package com.wwz.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.MybatisConfiguration;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.spring.MybatisMapperRefresh;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import io.seata.rm.datasource.DataSourceProxy;
import io.seata.spring.annotation.GlobalTransactionScanner;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.lang.Nullable;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.Optional;
import java.util.stream.Stream;

@org.springframework.context.annotation.Configuration
@EnableTransactionManagement
public class DruidDataSourceConfig {

    private final static ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();


    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptorImpl().setLocalPage(true);
    }




    //destroy-method="close"的作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用.
    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        return dataSource;
    }
    @Autowired
    private DataSource dataSource;

    @Bean
    public MybatisConfiguration mybatisConfiguration(){
        MybatisConfiguration configuration = new MybatisConfiguration();
        return configuration;
    }

  /*  @Bean
    public GlobalConfig globalConfig(){
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setAuthor("wwz");
        globalConfig.
    }*/
   /* @Bean
    public SqlSessionFactory sqlSessionFactory(){
        SqlSessionFactory sqlSessionFactoryBean = new DefaultSqlSessionFactory(mybatisConfiguration());
        return sqlSessionFactoryBean;
    }*/

    @Bean
    public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(){
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setConfiguration(mybatisConfiguration());
        sqlSessionFactoryBean.setMapperLocations(resolveMapperLocations(new String[]{"com/*/mapper/*.xml"}));
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{paginationInterceptor()});
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        sqlSessionFactoryBuilder.build(mybatisConfiguration());
        sqlSessionFactoryBean.setSqlSessionFactoryBuilder(sqlSessionFactoryBuilder);
        //sqlSessionFactoryBean.setGlobalConfig();
       return sqlSessionFactoryBean;
    }
    @Bean
    public MybatisMapperRefresh mybatisMapperRefresh(){
        Resource[] resources = resolveMapperLocations(new String[]{"com/*/mapper/*.xml"});
        MybatisMapperRefresh mybatisMapperRefresh = null;
        try {
            mybatisMapperRefresh = new MybatisMapperRefresh(resources, sqlSessionFactoryBean().getObject(),10,30,true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mybatisMapperRefresh;
    }

    private Resource[] resolveMapperLocations(String[] mapperLocations) {
        return Stream.of(Optional.ofNullable(mapperLocations).orElse(new String[0]))
                .flatMap(location -> Stream.of(getResources(location))).toArray(Resource[]::new);
    }
    private Resource[] getResources(String location) {
        try {
            return resourceResolver.getResources(location);
        } catch (IOException e) {
            return new Resource[0];
        }
    }
    /*@Bean
    @ConditionalOnBean(DataSource.class)
    public DataSourceProxy dataSourceProx() {
        return new DataSourceProxy(dataSource);
    }*/
/*    @Bean
    @ConditionalOnBean(DataSource.class)
    public GlobalTransactionScanner globalTransactionScanner() {
        return new GlobalTransactionScanner(txServiceGroup);
    }*/


    @Bean
    @ConditionalOnBean(DataSource.class)
    public DataSourceTransactionManager dataSourceTransactionManager() {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);
        dataSourceTransactionManager.setRollbackOnCommitFailure(true);
        return dataSourceTransactionManager ;
    }



}
